﻿using DevComponents.DotNetBar;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VMind.Global;

namespace PPIDesktopClient.Controls.QueryPolygonTree
{
    public partial class QueryPolygonTree : UserControl
    {
        #region Private Members
        private bool m_ExpandAll = false;
        private List<EUtility_Polygon> m_Polygons;
        private List<EUtility_Query_Type> m_QueryTypes;
        private List<EUtility_QueryPolygon> m_QueryPolygons;
        #endregion

        #region Private Methods
        private void AddNodes()
        {
            try
            {
                tvwMain.Nodes.Add(new TreeNodeEx("p" + Int32.MinValue.ToString(), "Static Polygons", GetChildNodes(Int32.MinValue)));
                TreeNode[] _TypeNodes = new TreeNode[m_QueryTypes.Count];
                for (Int32 i = 0; i < m_QueryTypes.Count; i++)
                    _TypeNodes[i] = new TreeNodeEx("t" + m_QueryTypes[i].Utility_Query_TypeId.ToString(), m_QueryTypes[i].Name + " Queries", new TreeNodeEx[0]);
                FillNodes(_TypeNodes);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private TreeNodeEx[] GetChildNodes(Int32 Utility_Polygon_ParentPolygonFk)
        {
            List<EUtility_Polygon> _Children = new List<EUtility_Polygon>();
            foreach (EUtility_Polygon _Polygon in m_Polygons)
                if (_Polygon.Utility_Polygon_ParentPolygon.Utility_PolygonId == Utility_Polygon_ParentPolygonFk)
                    _Children.Add(_Polygon);

            TreeNodeEx[] _ChildrenNodes = new TreeNodeEx[_Children.Count];
            for (Int32 i = 0; i < _Children.Count; i++)
                _ChildrenNodes[i] = new TreeNodeEx("p" + _Children[i].Utility_PolygonId.ToString(), _Children[i].Name, GetChildNodes(_Children[i].Utility_PolygonId));

            return _ChildrenNodes;
        }
        private void FillNodes(TreeNode[] typeNodes)
        {
            foreach (TreeNode _TypeNode in typeNodes)
                tvwMain.Nodes.Add(_TypeNode);
            bool _Found;
            foreach (EUtility_QueryPolygon _QueryPolygon in m_QueryPolygons)
            {
                _Found = false;
                for (Int32 i = 0; i < tvwMain.Nodes.Count && !_Found; i++)
                    if ("t" + _QueryPolygon.Utility_Query_Type.Utility_Query_TypeId.ToString() == ((TreeNodeEx)tvwMain.Nodes[i]).Value)
                    {
                        tvwMain.Nodes[i].Nodes.Add(new TreeNodeEx("q" + _QueryPolygon.Utility_QueryPolygonId.ToString(), _QueryPolygon.Name, new TreeNodeEx[0]));
                        _Found = true;
                    }
            }
        }
        #endregion

        #region Public Properties
        public bool ExpandAll
        {
            get { return m_ExpandAll; }
            set { m_ExpandAll = value; }
        }
        public List<EUtility_Polygon> Polygons
        {
            get { return m_Polygons; }
            set { m_Polygons = value; }
        }
        public List<EUtility_QueryPolygon> QueryPolygons
        {
            get { return m_QueryPolygons; }
            set { m_QueryPolygons = value; }
        }
        public List<EUtility_Query_Type> QueryTypes
        {
            get { return m_QueryTypes; }
            set { m_QueryTypes = value; }
        }
        public TreeNodeEx SelectedItem
        {
            get { return (TreeNodeEx)tvwMain.SelectedNode; }
            set { tvwMain.SelectedNode = value; }
        }
        #endregion

        #region Public Methods
        public override void Refresh()
        {
            //Suppress repainting the TreeView until all the objects have been created.
            tvwMain.BeginUpdate();
            //Clear the TreeView each time the method is called.
            tvwMain.Nodes.Clear();

            AddNodes();

            if (m_ExpandAll)
                tvwMain.ExpandAll();

            //Begin repainting the TreeView.
            tvwMain.EndUpdate();
        }
        public void SelectPolygons(List<EUtility_Polygon> selectedPolygons)
        {
            List<Int32> _SelectedIds = new List<Int32>();
            foreach (EUtility_Polygon _Polygon in selectedPolygons)
                _SelectedIds.Add(_Polygon.Utility_PolygonId);
            SelectPolygons(_SelectedIds);
        }
        public void SelectPolygons(List<Int32> selectedIds)
        {
            tvwMain.BeginUpdate();
            foreach (TreeNodeEx _Node in tvwMain.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "p" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_Polygons.Add(new EUtility_Polygon(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckPolygonChildren(selectedIds, _Node);
            }
            tvwMain.EndUpdate();
        }
        void CheckPolygonChildren(List<Int32> selectedIds, TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "p" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_Polygons.Add(new EUtility_Polygon(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckPolygonChildren(selectedIds, _Node);
            }
        }
        public void SelectQueryPolygons(List<EUtility_QueryPolygon> selectedQueryPolygons)
        {
            List<Int32> _SelectedIds = new List<Int32>();
            foreach (EUtility_QueryPolygon _QueryPolygon in selectedQueryPolygons)
                _SelectedIds.Add(_QueryPolygon.Utility_QueryPolygonId);
            SelectQueryPolygons(_SelectedIds);
        }
        public void SelectQueryPolygons(List<Int32> selectedIds)
        {
            tvwMain.BeginUpdate();
            foreach (TreeNodeEx _Node in tvwMain.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "q" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_QueryPolygons.Add(new EUtility_QueryPolygon(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckQueryChildren(selectedIds, _Node);
            }
            tvwMain.EndUpdate();
        }
        void CheckQueryChildren(List<Int32> selectedIds, TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                if (!_Node.Checked && _Node.Value.Substring(0, 1) == "q" && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value.Substring(1))))
                {
                    _Node.Checked = true;
                    m_QueryPolygons.Add(new EUtility_QueryPolygon(ConvertHelper.ToInt32(_Node.Value.Substring(1))));
                }
                if (_Node.Nodes.Count > 0)
                    CheckQueryChildren(selectedIds, _Node);
            }
        }
        void CheckChildren(TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                _Node.Checked = parentNode.Checked;
                if (_Node.Nodes.Count > 0)
                    CheckChildren(_Node);
            }
        }

        #endregion

        #region Constructor
        public QueryPolygonTree()
        {
            InitializeComponent();
            m_Polygons = new List<EUtility_Polygon>();
            m_QueryTypes = new List<EUtility_Query_Type>();
            m_QueryPolygons = new List<EUtility_QueryPolygon>();
        }
        #endregion

        #region Events
        public event TreeViewEventHandler CheckedChange;

        public event EventHandler ClickedNewQuery;
        public event EventHandler ClickedEditQuery;
        public event EventHandler ClickedDeleteQuery;

        public event EventHandler ClickedNew;
        public event EventHandler ClickedEdit;
        public event EventHandler ClickedDelete;
        public event EventHandler SelectedPolygon;
        public event EventHandler SelectedQuery;
        public event EventHandler DeselectedQuery;


        private void OnDeselectedQuery(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (DeselectedQuery != null)
            {
                DeselectedQuery(o, e);
            }
        }
        private void OnSelectedPolygon(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (SelectedPolygon != null)
            {
                SelectedPolygon(o, e);
            }
        }
        private void OnSelectedQuery(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (SelectedQuery != null)
            {
                SelectedQuery(o, e);
            }
        }

        private void OnClickedNew(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNew != null)
            {
                ClickedNew(o, e);
            }
        }
        private void OnClickedEdit(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDelete(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }


        private void OnClickedNewQuery(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNewQuery != null)
            {
                ClickedNewQuery(o, e);
            }
        }
        private void OnClickedEditQuery(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditQuery != null)
            {
                ClickedEditQuery(o, e);
            }
        }
        private void OnClickedDeleteQuery(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDeleteQuery != null)
            {
                ClickedDeleteQuery(o, e);
            }
        }


        private void OnCheckedChanged(object child, TreeViewEventArgs e)
        {
            // Safely invoke an event:
            if (CheckedChange != null)
                CheckedChange(child, e);
        }

        void tvwMain_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            TreeNodeEx _Node = (TreeNodeEx)e.Node;
            switch (_Node.Value.Substring(0, 1))
            {
                case "p":
                    foreach (EUtility_Polygon _Polygon in m_Polygons)
                        if (_Polygon.Utility_PolygonId == ConvertHelper.ToInt32(_Node.Value.Substring(1)))
                            OnCheckedChanged(_Polygon, e);
                    break;
                case "t":
                    foreach (EUtility_Query_Type _Type in m_QueryTypes)
                        if (_Type.Utility_Query_TypeId == ConvertHelper.ToInt32(_Node.Value.Substring(1)))
                            OnCheckedChanged(_Type, e);
                    break;
                case "q":
                    foreach (EUtility_QueryPolygon _QueryPolygon in m_QueryPolygons)
                        if (_QueryPolygon.Utility_QueryPolygonId == ConvertHelper.ToInt32(_Node.Value.Substring(1)))
                            OnCheckedChanged(_QueryPolygon, e);
                    break;
            }
            CheckChildren(_Node);
        }

        private void cmiNewPolygonQuery_Click(object sender, EventArgs e)
        {
            OnClickedNewQuery(sender, e);
        }

        private void cmiEditPolygonQuery_Click(object sender, EventArgs e)
        {
            OnClickedEditQuery(sender, e);
        }

        private void cmiDeletePolygonQuery_Click(object sender, EventArgs e)
        {
            OnClickedDeleteQuery(sender, e);
        }

        private void cmiNewPolygon_Click(object sender, EventArgs e)
        {
            OnClickedNew(sender, e);
        }

        private void cmiEditPolygon_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }

        private void cmiDeletePolygon_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, e);
        }

        private void cmbPolygons_PopupOpen(object sender, PopupOpenEventArgs e)
        {
            TreeNodeEx selectedNode = (TreeNodeEx)tvwMain.SelectedNode;
            if (selectedNode == null || selectedNode.Level == 0)
            {
                e.Cancel = true;
            }
            else
            {
                cmiEditPolygonQuery.Visible = false;
                cmiNewPolygonQuery.Visible = false;
                cmiDeletePolygonQuery.Visible = false;
                cmiDeletePolygon.Visible = false;
                cmiEditPolygon.Visible = false;
                cmiNewPolygon.Visible = false;


                switch (selectedNode.Value.Substring(0, 1))
                {

                    case "p":
                        cmiDeletePolygon.Visible = true;
                        cmiEditPolygon.Visible = true;
                        cmiNewPolygon.Visible = true;
                        break;
                    case "q":
                        cmiEditPolygonQuery.Visible = true;
                        cmiNewPolygonQuery.Visible = true;
                        cmiDeletePolygonQuery.Visible = true;
                        break;
                    default:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private void tvwMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNodeEx node = (TreeNodeEx)e.Node;
            if (node != null && node.Level != 0)
            {
                tvwMain.SelectedNode = node;

                if (node.Value.Substring(0, 1) == "p")
                    OnSelectedPolygon(sender, e);

                if (node.Value.Substring(0, 1) == "q")
                    OnSelectedQuery(sender, e);
            }
            else
            {
                OnDeselectedQuery(sender, e);
            }
        }
        private void tvwMain_Leave(object sender, EventArgs e)
        {
            OnDeselectedQuery(sender, e);
        }

        #endregion













    }
}
